package com.coffee.socketio.handler;

import com.coffee.socketio.CoffeeSocketioException;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class HandlerMapping {

    private final static Map<String, SocketioTextHandler> socketHandlerMapping = new HashMap<>();
    private final static Map<String, SocketioJsonHandler> socketJsonHandlerMapping = new HashMap<>();

    @Getter
    private final static HandlerParameter handlerParameter = new HandlerParameter();


    //------------get--------------------------
    public static SocketioTextHandler getSocketHandler(String event){
        if (socketHandlerMapping.isEmpty()){
            throw new CoffeeSocketioException("Initialize textCommandHandlerMapping analysis handler error!");
        }
        return socketHandlerMapping.get(event);
    }

    public static SocketioJsonHandler getMochaSocketTextJsonHandler(String event){
        if (socketJsonHandlerMapping.isEmpty()){
            throw new CoffeeSocketioException("Initialize SocketJsonHandlerMapping analysis handler error!");
        }
        return socketJsonHandlerMapping.get(event);
    }

    public static SocketioConnectHandler getSocketConnectHandler(){
        return handlerParameter.getSocketConnectHandler();
    }

    public static SocketioCloseHandler getSocketCloseHandler(){
        return handlerParameter.getSocketCloseHandler();
    }
    public static SocketioUserValidHandler getSocketUserValidHandler(){
        return handlerParameter.getSocketUserValidHandler();
    }


    //---------------------------------initialize------------------------
    public static void initializeSocketConnectHandler(SocketioConnectHandler socketConnectHandler){
        handlerParameter.setSocketConnectHandler(socketConnectHandler);
    }
    public static void initializeSocketClosetHandler(SocketioCloseHandler socketCloseHandler){
        handlerParameter.setSocketCloseHandler(socketCloseHandler);
    }
    public static void initializeSocketUserValidHandler(SocketioUserValidHandler socketUserValidHandler){
        handlerParameter.setSocketUserValidHandler(socketUserValidHandler);
    }

    public static void initializeSocketTextHandler(Map<String, SocketioTextHandler<?>> handlerMap)throws Exception{
       if (handlerMap == null || handlerMap.isEmpty()){
           return;
       }
        socketHandlerMapping.putAll(handlerMap);
    }
    public static void initializeSocketTextJsonHandler(Map<String, SocketioJsonHandler> handlerMap)throws Exception{
       if (handlerMap == null || handlerMap.isEmpty()){
           return;
       }
        socketJsonHandlerMapping.putAll(handlerMap);
    }

}
